package com.itheima.exerciseTest.method10_19.method19;

import java.io.*;
import java.util.ArrayList;

public class UserDao {
    public boolean userRegister(String username, String password) {
        User u = new User();
        u.setUsername(username);
        u.setPassword(password);

        // 判断f.txt中是否有问有ArrayList集合对象
        ObjectInputStream ois = null;
        boolean boo = false;
        boo = get();

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("backBurner\\f.txt"));
            if (boo == false) {
                ArrayList<User> al = new ArrayList<>();
                al.add(u);
                oos.writeObject(al);
            } else {
                Object o = ois.readObject();
                ArrayList<User> al = (ArrayList<User>) o;
                al.add(u);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return true;
    }

    // 登录
    public boolean userLogin(String username, String password) {
        boolean boo = get();
        if (boo == false) {
            return false;
        } else {
            ArrayList<User> al = null;
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream("backBurner\\f.txt"));
                Object o = ois.readObject();
                al = (ArrayList<User>) o;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            for (User u : al) {
                String username1 = u.getUsername();
                String password1 = u.getPassword();
                if (username1.equals(username) && password1.equals(password)) {
                    return true;
                }
            }
            return false;
        }
    }

    public boolean get() {
        ObjectInputStream ois = null;
        boolean boo = true;
        try {
            ois = new ObjectInputStream(new FileInputStream("backBurner\\f.txt"));
            ois.readObject();
        } catch (EOFException e) {
            boo = false;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return boo;
    }

}
